package com.onetech.adrewards.free.dialogs;

import android.app.Activity;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.fedorvlasov.lazylist.ImageLoader;
import com.google.analytics.tracking.android.Fields;
import com.google.analytics.tracking.android.MapBuilder;
import com.google.analytics.tracking.android.Tracker;
import com.onetech.adrewards.free.R;
import com.onetech.adrewards.free.config.AppConfig;
import com.onetech.adrewards.free.objects.AdDTO;
import com.onetech.adrewards.free.utils.AdrewadsUtils;
import com.zuzu.dialogs.BaseDialog;

public class AdRewardDetailDialog extends BaseDialog {
	private TextView newsContent=null;
	private ImageView mImageCover=null;
	private TextView mTitleAd=null;
	private TextView mChargeType=null;
	private TextView mAdPont=null;
	private TextView mCountTypeForUser=null;
	private TextView mAppWaitTime=null;
	
	private Button mDownloadBtn=null;
	private String mInstallUrl="";
	private AdDTO mData=null;
	
	private Tracker mTracker;
	private Activity mActivity=null;
	
	
	
	public AdRewardDetailDialog(Activity context,AdDTO data){
		super(context, context.getString(R.string.appli_detail_title),
				TYPE_NO_BUTTON, R.layout.appli_detail_layout);
		mActivity=context;
		if(data==null){
			dismiss();
		}
		mData=data;
		
		newsContent=(TextView)findViewById(R.id.mainContent);
		mImageCover=(ImageView)findViewById(R.id.imageCover);
		mTitleAd=(TextView)findViewById(R.id.titleAd);
		mChargeType=(TextView)findViewById(R.id.chargeExp);
		mAdPont=(TextView)findViewById(R.id.adPoint);
		mCountTypeForUser=(TextView)findViewById(R.id.countTypeForUser);
		mAppWaitTime=(TextView)findViewById(R.id.appWaitTime);
		
		mDownloadBtn=(Button)findViewById(R.id.downloadBtn);
		
		try {
			mTitleAd.setText(data.getContent_name());
			mChargeType.setText(AdrewadsUtils.getChargeTypeText(context,data.getCharge_tpe()) + AdrewadsUtils.getAction_Cate_ID_Text(context,data.getAction_cate_id()));
			mAdPont.setText(data.getMax_point()+ " pt");
			String content=data.getContent_exp();
			newsContent.setText(Html.fromHtml(content));
			mCountTypeForUser.setText(Html.fromHtml(data.getCount_type_for_user()));
			mAppWaitTime.setText(data.getPb_wait_time());
			
			
			
			mDownloadBtn.setOnClickListener(this);
			
			mInstallUrl=data.getAd_tag_of_point_back();			
			ImageLoader.getInstance(context).DisplayImage(data.getBanner_img_1(), mImageCover);
			
		} catch (Exception e) {
		}
	}	
	@Override
	public void onClick(View v) {
		int id=v.getId();
		switch (id) {
		case R.id.downloadBtn:
			doAction();
			break;

		default:
			super.onClick(v);
			break;
		}
	}
	private void doAction(){
//		http://mv.mobadme.jp/spg/spnew/{â‘ m_ownerID}/{â‘¡m_ID}/detail_mv.php?
//			user_id    =     {â‘¢media user}
//			&crypt     =     {â‘£æš—å�·åŒ–ã‚­ãƒ¼}
//			&ad         =     {â‘¤video ad id}
		//check has video
		//mInstallUrl="http://mv.mobadme.jp/spg/spnew/558/2835/detail_mv.php?user_id=9087"+"&crypt="+getCrypt()+"&ad="+mData.getAd_id();
	
		try {
			AdrewadsUtils.checkUId();
			if(mData.getHas_video().trim().equals("1") || mData.getContent_manage_category().trim().equals("6")){
				mInstallUrl="http://mv.mobadme.jp/spg/spnew/558/2835/detail_mv.php?user_id="+AppConfig.uId+"&crypt="+AdrewadsUtils.getCrypt()+"&ad="+mData.getAd_id();
			}else{
				mInstallUrl=mData.getAd_tag_of_point_back();	
			}
			AdrewadsUtils.go2Web(getContext(), mInstallUrl);			
		} catch (Exception e) {
		}
	}
	@Override
    public void onStart() {
        super.onStart();
        try {
            mTracker.set(Fields.SCREEN_NAME, mActivity.getString(R.string.ExchangeFragment));            
            mTracker.send(MapBuilder.createAppView().build());            
		} catch (Exception e) {
		}
    }
}
